package com.aplink.generic.widgets;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.aplink.generic.GenericApplication;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.bitmap.ImageCache;
import com.aplink.generic.bitmap.ImageFetcher;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.ImageViewSpec;
import com.aplink.generic.util.ResourcesHelper;

public class ApLinkImageView extends ApLinkBaseView {
	private static final String IMAGE_CACHE_DIR = "images";
	private final ImageView mImageView;
	private final ImageViewSpec mImageViewSpec;

	public ApLinkImageView(final GenericFragment genericFragment,
	        final ControlSpec controlSpec, final View view, Bundle bundle) {
		super(genericFragment, controlSpec, view, bundle);
		this.mImageView = (ImageView) view;
		this.mImageViewSpec = (ImageViewSpec) controlSpec;
		final Context context = GenericApplication.getInstance()
		        .getApplicationContext();
		if ((this.mImageViewSpec.getImageWidth() + this.mImageViewSpec
		        .getImageHeight()) == 0) {
			ImageFetcher.getInstance();
		} else {
			ImageFetcher.getInstance(context,
			        this.mImageViewSpec.getImageWidth(),
			        this.mImageViewSpec.getImageHeight());
		}
		final ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(
		        context, ApLinkImageView.IMAGE_CACHE_DIR);
		cacheParams.setMemCacheSizePercent(0.25f);
		ImageFetcher.getInstance().addImageCache(getChildFragmentManager(),
		        cacheParams);
		ImageFetcher.getInstance().setImageFadeIn(true);
	}

	@Override
	public void clear() {
		this.mImageView.setImageDrawable(null);
	}

	@Override
	public GenericAttribute getAttribute() {
		return null;
	}

	@Override
	public GenericDictionary<String, GenericAttribute> getListAttributes() {
		return null;
	}

	@Override
	public ImageView getView() {
		return this.mImageView;
	}

	@Override
	protected void initViewProperties() {
		final boolean isVisibility = this.mImageViewSpec.isVisibility();
		if (!isVisibility) {
			this.mImageView.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public void languageChanged() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void loadBindingObject() {
		String imageUrl = getPassedContent();
		if (imageUrl == null) {
			final String dataObj = this.mImageViewSpec.getDataObject();
			final String mappingName = this.mImageViewSpec.getMappingName();
			imageUrl = getPassedObject().getValue(
			        String.format("%s.%s", dataObj, mappingName)).toString();
		}
		if (imageUrl != null) {
			// this.mImageFetcher.setReadOnly(this.mImageViewSpec.isReadOnly());
			// this.mImageFetcher.setMaxZoom(this.mImageViewSpec.getMaxZoom());
			// this.mImageFetcher.setMinZoom(this.mImageViewSpec.getMinZoom());
			if (!TextUtils.isEmpty(this.mImageViewSpec.getDefaultImage())) {
				final int resId = ResourcesHelper
				        .getDrawableIdByName(this.mImageViewSpec
				                .getDefaultImage());
				ImageFetcher.getInstance().setLoadingImage(resId);
			}
			ImageFetcher.getInstance().loadImage(imageUrl, this.mImageView);
		}
	}

	@Override
	protected void loadDefaultObject() {
	}

	@Override
	protected void setEventListener() {

	}

	@Override
	public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
		throw new UnsupportedOperationException(this.getClass().getSimpleName()
		        + " is not support this method");
	}

	@Override
	public void setOnClickListener(final OnClickListener onClickListener) {
		throw new UnsupportedOperationException(this.getClass().getSimpleName()
		        + " is not support this method");
	}
}
